package com.serene.authority.admin.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serene.authority.admin.entity.vo.JobQueryVo;
import com.serene.authority.admin.entity.MyJob;
import com.serene.authority.admin.mapper.JobMapper;
import com.serene.authority.admin.mapper.UserJobMapper;
import com.serene.authority.admin.service.JobService;
import com.serene.authority.admin.service.UserJobService;
import com.serene.authority.common.exceptionhandler.MyException;
import com.serene.authority.common.utils.Result;
import com.serene.authority.common.utils.ResultCode;
import com.serene.authority.common.utils.UserConstants;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.hutool.core.date.DateTime.now;

/**
 * @author serene
 * @createTime 2020/8/19
 */
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, MyJob> implements JobService {

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private UserJobMapper userJobMapper;

    @Autowired
    private UserJobService userJobService;

    @Override
    public Result<MyJob> getJobAll(Integer offectPosition, Integer limit, JobQueryVo jobQueryDto) {
        Page page = PageHelper.offsetPage(offectPosition, limit);
        List<MyJob> fuzzyJob = jobMapper.getFuzzyJob(jobQueryDto.getQueryName(), jobQueryDto.getQueryStatus());
        return Result.ok().count(page.getTotal()).data(fuzzyJob).code(ResultCode.TABLE_SUCCESS);
    }

    @Override
    public int insertJob(MyJob job) {
        job.setCreateTime(now());
        job.setUpdateTime(now());
        return jobMapper.insert(job);
    }

    /**
     * 校验岗位名称是否唯一
     *
     * @param job 岗位信息
     * @return 结果
     */
    @Override
    public String checkJobNameUnique(MyJob job) {
        MyJob info = checkJobNameUnique(job.getJobName());
        if (ObjectUtil.isNotEmpty(info) && !info.getJobId().equals(job.getJobId())) {
            return UserConstants.JOB_NAME_NOT_UNIQUE;
        }
        return UserConstants.JOB_NAME_UNIQUE;
    }

    @Override
    public MyJob getJobById(Integer jobId) {
        QueryWrapper<MyJob> wrapper = new QueryWrapper<>();
        wrapper.eq("job_id", jobId);
        return jobMapper.selectOne(wrapper);
    }

    @Override
    public int deleteJobByIds(String ids) throws MyException {
        Integer[] jobIds = Convert.toIntArray(ids);
        for (Integer jobid : jobIds) {
            MyJob job = getJobById(jobid);
            if (countUserJobById(jobid) > 0) {
                throw new MyException(ResultCode.ERROR, job.getJobName() + "已分配,不能删除");
            }
        }
        return jobMapper.deleteJobByIds(jobIds);
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param jobId 岗位ID
     * @return
     */
    @Override
    public int countUserJobById(Integer jobId) {
        return userJobService.queryUserJobCountByJobId(jobId);
    }

    @Override
    public List<MyJob> selectJobAll() {
        return jobMapper.selectList(null);
    }

    @Override
    public List<MyJob> selectJobsByUserId(Integer userId) {
        List<MyJob> userJobs = jobMapper.selectJobsByUserId(userId);
        List<MyJob> jobs = jobMapper.selectList(null);
        for (MyJob job : jobs) {
            for (MyJob userJob : userJobs) {
                if (job.getJobId().equals(userJob.getJobId())) {
                    job.setFlag(true);
                    break;
                }
            }
        }
        return jobs;
    }

    @Override
    public int updateJob(MyJob myJob) {
        return jobMapper.updateJob(myJob);
    }

    @Override
    public int changeStatus(MyJob myJob) {
        return jobMapper.updateJob(myJob);
    }


    /**
     * 校验岗位名称
     *
     * @param name
     * @return
     */
    @Override
    public MyJob checkJobNameUnique(String name) {
        QueryWrapper<MyJob> wrapper = new QueryWrapper<>();
        wrapper.eq("job_name", name);
        wrapper.last("limit 1");
        return jobMapper.selectOne(wrapper);
    }

}
